import Admin from "../models/admin";

class AdminService {
    async login(username: string, password: string): Promise<boolean | Admin> {
        try {
            const admin = await Admin.findOne({
                where: {
                    username,
                    password
                }
            });
            if (admin) {
                return admin;
            } else {
                return false;
            }
        } catch (e) {
            return false;
        }
    }

    async findById(id: number): Promise<Admin | null> {
        try {
            const admin = await Admin.findByPk(id);
            return admin;
        } catch (e) {
            return null;
        }
    }
    async init() {
        await Admin.sync({ force: true });
    }

    async create(username: string, password: string, email: string): Promise<boolean> {
        try {
            await Admin.create({
                username,
                password,
                email
            });
            return true;
        } catch (e) {
            return false;
        }
    }
}

export default AdminService;